package com.lineage.data.item_etcitem.doll;

import com.lineage.config.ConfigOther;
import com.lineage.data.executor.ItemExecutor;
import com.lineage.server.datatables.DollPowerTable;
import com.lineage.server.datatables.ItemTable;
import com.lineage.server.datatables.NpcTable;
import com.lineage.server.datatables.SupperDollTable;
import com.lineage.server.model.Instance.L1DollInstance;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1War;
import com.lineage.server.serverpackets.S_ItemName;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.templates.L1Doll;
import com.lineage.server.templates.L1Item;
import com.lineage.server.templates.L1Npc;
import com.lineage.server.templates.L1SupperDoll;
import com.lineage.server.timecontroller.server.ServerWarExecutor;
import com.lineage.server.world.WorldItem;
import com.lineage.server.world.WorldWar;
import com.mchange.v2.codegen.bean.BeanExtractingGeneratorExtension;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Magic_Doll extends ItemExecutor {
//  private Map<Integer, L1DollInstance> _dolls2 = new HashMap<>();
//  
//  private Map<Integer, L1DollInstance> _dolls3 = new HashMap<>();
  
  public static ItemExecutor get() {
    return new Magic_Doll();
  }
  
  public void execute(int[] data, L1PcInstance pc, L1ItemInstance item) {
    int itemId = item.getItemId();
    int itemobj = item.getId();
    useMagicDoll(pc, itemId, itemobj);
  }
  
  private void useMagicDoll(L1PcInstance pc, int itemId, int itemObjectId) {
	  
	    if (pc.getDoll(itemObjectId) != null) {
		      pc.getDoll(itemObjectId).deleteDoll();
		      return;
		    } 
	    if (pc.isInvisble())
		      return; 
	    int castle_id = 1;
	    while (castle_id < 8) {
	      if (!ConfigOther.WAR_DOLL && ServerWarExecutor.get().isNowWar(castle_id) && pc.getMapId() == 4) {
	        pc.sendPackets((ServerBasePacket)new S_ServerMessage("攻城战期间无法使用娃娃!!"));
	        return;
	      } 
	      castle_id++;
	    } 
//	    if (!pc.getDolls().isEmpty()) {
//		      Iterator<L1DollInstance> iter = pc.getDolls().values().iterator();
//		      while (iter.hasNext()) {
//		        L1DollInstance doll = iter.next();
//		        L1Doll type2 = DollPowerTable.get()
//		          .get_type(pc.getInventory().getItem(doll.getItemObjId()).getItemId());
//		        if (type2 != null) {
//		          if (type2.get_mode() == 0)
//		            this._dolls2.put(Integer.valueOf(doll.getItemObjId()), doll); 
//		          if (type2.get_mode() != 1)
//		            continue; 
//		          this._dolls3.put(Integer.valueOf(doll.getItemObjId()), doll);
//		        } 
//		      } 
//		    } 
	    L1ItemInstance item = pc.getInventory().getItem(itemObjectId);
	    
	    if (pc.getDolls().isEmpty()) {
	    	Boolean isSupperDoll = false;
	    	if (!SupperDollTable.get().get_powerMap().keySet().isEmpty()) {
//	    		for (Integer key : SupperDollTable.get().get_powerMap().keySet()) {
//					if (itemId == key) {
//						isSupperDoll = true;
//						break;
//					}
//	    		}
	    		    		
				Set<Integer> keyset = SupperDollTable.get().get_powerMap().keySet();
				Iterator<Integer> iterator = keyset.iterator();
				while (iterator.hasNext()) {
				    Integer key = iterator.next();
				    if (key == itemObjectId) {
				    	isSupperDoll = true;
				    } 
				}
	    		if (isSupperDoll) {
					supperDoll(pc, itemId, itemObjectId);
//					item.setEquipped(true);
				    pc.sendPackets((ServerBasePacket)new S_ItemName(item));
				}else {
					normalDoll(pc, itemId, itemObjectId);
					pc.sendPackets((ServerBasePacket)new S_ItemName(item));
//					item.setEquipped(true);
				}
			}
		}else {
	    
		pc.sendPackets((ServerBasePacket)new S_SystemMessage("你不能携带更多娃娃"));
		}
  }
  
  
  private void normalDoll(L1PcInstance pc, int itemId, int itemObjectId) {
	    L1Doll type = DollPowerTable.get().get_type(itemId);
//
//
//	    if (this._dolls2.size() >= ConfigOther.dollcount && type.get_mode() == 0) {
//	      pc.sendPackets((ServerBasePacket)new S_SystemMessage("已携带:" + this._dolls2.size() + "只魔法娃娃，不能带更多了"));
//	      this._dolls2.clear();
//	      this._dolls3.clear();
//	      return;
//	    } 
//	    if (this._dolls3.size() >= ConfigOther.dollcount1 && type.get_mode() == 1) {
//	      pc.sendPackets((ServerBasePacket)new S_SystemMessage("已携带:" + this._dolls3.size() + "只补助娃娃，不能带更多了"));
//	      this._dolls2.clear();
//	      this._dolls3.clear();
//	      return;
//	    } 
	    if (!pc.getDolls().isEmpty()) {
	      Iterator<L1DollInstance> iter = pc.getDolls().values().iterator();
	      while (iter.hasNext()) {
	        L1DollInstance doll = iter.next();
	        if (pc.getInventory().getItem(doll.getItemObjId()).getItemId() == itemId) {
	          pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\fY不能携带相同的娃娃"));
	          return;
	        } 
	      } 
	    } 
	    if (!ConfigOther.WAR_DOLL && pc.getClan() != null) {
	      boolean inWar = false;
	      if (pc.getClan().getCastleId() != 0) {
	        if (ServerWarExecutor.get().isNowWar(pc.getClan().getCastleId()))
	          inWar = true; 
	      } else {
	        List<L1War> warList = WorldWar.get().getWarList();
	        Iterator<L1War> iter2 = warList.iterator();
	        while (iter2.hasNext()) {
	          L1War war = iter2.next();
	          if (war.checkClanInWar(pc.getClan().getClanName())) {
	            inWar = true;
	            break;
	          } 
	        } 
	      } 
	      if (inWar) {
	        pc.sendPackets((ServerBasePacket)new S_ServerMessage(1531));
	        return;
	      } 
	    } 
	    boolean iserror = false;
	    if (type != null) {
	      if (type.get_need() != null) {
	        int[] itemids = type.get_need();
	        int[] counts = type.get_counts();
	        int i = 0;
	        while (i < itemids.length) {
	          if (!pc.getInventory().checkItem(itemids[i], counts[i])) {
	            L1Item temp = ItemTable.get().getTemplate(itemids[i]);
	            pc.sendPackets((ServerBasePacket)new S_ServerMessage(337, temp.getNameId()));
	            iserror = true;
	          } 
	          i++;
	        } 
	        if (!iserror) {
	          i = 0;
	          while (i < itemids.length) {
	            pc.getInventory().consumeItem(itemids[i], counts[i]);
	            i++;
	          } 
	        } 
	      } 
	      if (!iserror) {


	        L1Npc template = NpcTable.get().getTemplate(71082);	        
	        L1DollInstance doll2 = new L1DollInstance(template, pc, itemObjectId, type, false);
			pc.addDoll(doll2);
	        L1ItemInstance item = WorldItem.get().getItem(Integer.valueOf(itemObjectId));
	        item.startEquipmentTimer(pc);
	        item.setEquipped(true);
	      } 
	    } 
  }
  
  private void supperDoll(L1PcInstance pc, int itemid, int itemObjectId) {
	  
	    L1SupperDoll type = SupperDollTable.get().get_type(itemObjectId);

	    if (pc.getDoll(itemObjectId) != null) {
	      pc.getDoll(itemObjectId).deleteDoll();
	      return;
	    } 
	    if (pc.isInvisble())
	      return; 
	    int castle_id = 1;
	    while (castle_id < 8) {
	      if (!ConfigOther.WAR_DOLL && ServerWarExecutor.get().isNowWar(castle_id) && pc.getMapId() == 4) {
	        pc.sendPackets((ServerBasePacket)new S_ServerMessage("攻城战期间无法使用娃娃!!"));
	        return;
	      } 
	      castle_id++;
	    } 
//	    if (!pc.getDolls().isEmpty()) {
//	      Iterator<L1DollInstance> iter = pc.getDolls().values().iterator();
//	      while (iter.hasNext()) {
//	        L1DollInstance doll = iter.next();
//	        L1Doll type2 = DollPowerTable.get()
//	          .get_type(pc.getInventory().getItem(doll.getItemObjId()).getItemId());
//	        if (type2 != null) {
//	          if (type2.get_mode() == 0)
//	            this._dolls2.put(Integer.valueOf(doll.getItemObjId()), doll); 
//	          if (type2.get_mode() != 1)
//	            continue; 
//	          this._dolls3.put(Integer.valueOf(doll.getItemObjId()), doll);
//	        } 
//	      } 
//	    } 
//	    if (this._dolls2.size() >= ConfigOther.dollcount && type.get_mode() == 0) {
//	      pc.sendPackets((ServerBasePacket)new S_SystemMessage("已携带:" + this._dolls2.size() + "只魔法娃娃，不能带更多了"));
//	      this._dolls2.clear();
//	      this._dolls3.clear();
//	      return;
//	    } 
//	    if (this._dolls3.size() >= ConfigOther.dollcount1 && type.get_mode() == 1) {
//	      pc.sendPackets((ServerBasePacket)new S_SystemMessage("已携带:" + this._dolls3.size() + "只补助娃娃，不能带更多了"));
//	      this._dolls2.clear();
//	      this._dolls3.clear();
//	      return;
//	    } 
	    if (!pc.getDolls().isEmpty()) {
	      Iterator<L1DollInstance> iter = pc.getDolls().values().iterator();
	      while (iter.hasNext()) {
	        L1DollInstance doll = iter.next();
	        if (pc.getInventory().getItem(doll.getItemObjId()).getItemId() == itemid) {
	          pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\fY不能携带相同的娃娃"));
	          return;
	        } 
	      } 
	    } 
	    if (!ConfigOther.WAR_DOLL && pc.getClan() != null) {
	      boolean inWar = false;
	      if (pc.getClan().getCastleId() != 0) {
	        if (ServerWarExecutor.get().isNowWar(pc.getClan().getCastleId()))
	          inWar = true; 
	      } else {
	        List<L1War> warList = WorldWar.get().getWarList();
	        Iterator<L1War> iter2 = warList.iterator();
	        while (iter2.hasNext()) {
	          L1War war = iter2.next();
	          if (war.checkClanInWar(pc.getClan().getClanName())) {
	            inWar = true;
	            break;
	          } 
	        } 
	      } 
	      if (inWar) {
	        pc.sendPackets((ServerBasePacket)new S_ServerMessage(1531));
	        return;
	      } 
	    } 
	    boolean iserror = false;
	    if (type != null) {
	      if (type.get_need() != null) {
	        int[] itemids = type.get_need();
	        int[] counts = type.get_counts();
	        int i = 0;
	        while (i < itemids.length) {
	          if (!pc.getInventory().checkItem(itemids[i], counts[i])) {
	            L1Item temp = ItemTable.get().getTemplate(itemids[i]);
	            pc.sendPackets((ServerBasePacket)new S_ServerMessage(337, temp.getNameId()));
	            iserror = true;
	          } 
	          i++;
	        } 
	        if (!iserror) {
	          i = 0;
	          while (i < itemids.length) {
	            pc.getInventory().consumeItem(itemids[i], counts[i]);
	            i++;
	          } 
	        } 
	      } 
	      if (!iserror) {
	        L1Npc template = NpcTable.get().getTemplate(71082);
	        L1DollInstance supperDoll = new L1DollInstance(template, pc, itemObjectId, type, false);
	        pc.addDoll(supperDoll);
	        L1ItemInstance item = WorldItem.get().getItem(Integer.valueOf(itemObjectId));
	        item.startEquipmentTimer(pc);
	        item.setEquipped(true);
	      } 
	    } 
  }
  
}
